<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java StringBuffer原理详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-radius: 8px;
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .concept-card {
            transition: all 0.3s ease;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.15);
        }
        .footer {
            background-color: #1a1a1a;
        }
        a:hover {
            color: #6e8efb;
        }
        .first-letter {
            font-size: 3.5rem;
            float: left;
            line-height: 1;
            margin-right: 0.5rem;
            margin-top: 0.2rem;
            color: #6e8efb;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6 font-serif">深入理解Java StringBuffer</h1>
            <p class="text-xl md:text-2xl mb-8 max-w-3xl mx-auto">探索可变字符串背后的原理与内存机制</p>
            <div class="flex justify-center space-x-4">
                <a href="#explanation" class="bg-white text-indigo-600 px-6 py-3 rounded-full font-medium hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-book mr-2"></i>原理详解
                </a>
                <a href="#visualization" class="bg-indigo-700 text-white px-6 py-3 rounded-full font-medium hover:bg-indigo-800 transition duration-300">
                    <i class="fas fa-project-diagram mr-2"></i>可视化图表
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Code Example Section -->
        <section id="code-example" class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-code text-indigo-500 text-2xl mr-3"></i>
                <h2 class="text-3xl font-bold text-gray-800">示例代码解析</h2>
            </div>
            
            <div class="code-block p-6 mb-8">
                <pre class="text-gray-100 overflow-x-auto">
                    <code class="language-java">
public static void main(String[] args) {
    StringBuffer sf = new StringBuffer("hi,");
    changeSf(sf);
    System.out.println(sf);
}
public static void changeSf(StringBuffer sf){
    sf.append("zhangsan");
}
                    </code>
                </pre>
            </div>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-8">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-info-circle text-blue-500"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-blue-700"><strong>运行结果：</strong>hi,zhangsan</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Explanation Section -->
        <section id="explanation" class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-yellow-500 text-2xl mr-3"></i>
                <h2 class="text-3xl font-bold text-gray-800">原理分析</h2>
            </div>
            
            <article class="prose max-w-none">
                <p><span class="first-letter">S</span>tringBuffer是Java中一个可变字符串类，与String不同，StringBuffer的内容可以被修改而不创建新的对象。在上面的示例中，我们清晰地看到了对象引用传递的特性。</p>
                
                <h3 class="text-2xl font-semibold mt-8 mb-4 text-gray-700">关键概念解析</h3>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="concept-card bg-white p-6 rounded-xl shadow-md border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-cube text-indigo-600"></i>
                            </div>
                            <h4 class="text-xl font-bold text-gray-800">对象引用传递</h4>
                        </div>
                        <p class="text-gray-600">Java中对象作为参数传递时，传递的是对象的引用（内存地址）。在示例中，sf的引用被传递给changeSf方法，因此方法内对对象的修改会影响原始对象。</p>
                    </div>
                    
                    <div class="concept-card bg-white p-6 rounded-xl shadow-md border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 p-2 rounded-full mr-3">
                                <i class="fas fa-exchange-alt text-green-600"></i>
                            </div>
                            <h4 class="text-xl font-bold text-gray-800">可变性</h4>
                        </div>
                        <p class="text-gray-600">StringBuffer是可变的字符串序列，其append()等方法会直接修改对象内容，而不像String那样每次操作都创建新对象。这使得StringBuffer在频繁修改字符串时效率更高。</p>
                    </div>
                </div>
                
                <h3 class="text-2xl font-semibold mt-8 mb-4 text-gray-700">内存机制</h3>
                <p>在示例代码执行过程中，StringBuffer对象在堆内存中创建，main方法中的sf变量和changeSf方法中的sf参数都指向同一个内存地址。因此，方法内对对象的修改会直接影响原始对象。</p>
            </article>
        </section>

        <!-- Visualization Section -->
        <section id="visualization" class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-project-diagram text-purple-500 text-2xl mr-3"></i>
                <h2 class="text-3xl font-bold text-gray-800">概念可视化</h2>
            </div>
            
            <div class="bg-white p-6 rounded-xl shadow-md">
                <div class="mermaid">
                    sequenceDiagram
                        participant Main
                        participant changeSf
                        participant Heap
                        
                        Main->>Heap: 创建StringBuffer("hi,")
                        Note right of Heap: 内存地址:0x1234
                        Main->>changeSf: 传递引用0x1234
                        changeSf->>Heap: 调用append("zhangsan")
                        Note right of Heap: 内容变为"hi,zhangsan"
                        changeSf-->>Main: 返回
                        Main->>Heap: 获取StringBuffer内容
                        Heap-->>Main: "hi,zhangsan"
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6 mt-8">
                <div class="bg-white p-6 rounded-xl shadow-md border-l-4 border-indigo-500">
                    <h4 class="font-bold text-lg mb-3 text-gray-800">1. 对象创建</h4>
                    <p class="text-gray-600">在堆内存中创建StringBuffer对象，内容初始化为"hi,"</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md border-l-4 border-blue-500">
                    <h4 class="font-bold text-lg mb-3 text-gray-800">2. 引用传递</h4>
                    <p class="text-gray-600">main方法将对象引用传递给changeSf方法</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md border-l-4 border-green-500">
                    <h4 class="font-bold text-lg mb-3 text-gray-800">3. 内容修改</h4>
                    <p class="text-gray-600">changeSf方法通过引用修改对象内容</p>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-balance-scale text-red-500 text-2xl mr-3"></i>
                <h2 class="text-3xl font-bold text-gray-800">String vs StringBuffer</h2>
            </div>
            
            <div class="overflow-x-auto">
                <table class="min-w-full bg-white rounded-lg overflow-hidden shadow">
                    <thead class="bg-gray-100">
                        <tr>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">特性</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">String</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">StringBuffer</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr>
                            <td class="py-3 px-4">可变性</td>
                            <td class="py-3 px-4 text-red-500">不可变</td>
                            <td class="py-3 px-4 text-green-500">可变</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">线程安全</td>
                            <td class="py-3 px-4">安全</td>
                            <td class="py-3 px-4 text-green-500">安全(同步方法)</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">性能</td>
                            <td class="py-3 px-4">修改操作慢</td>
                            <td class="py-3 px-4 text-green-500">修改操作快</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">内存使用</td>
                            <td class="py-3 px-4">频繁修改时高</td>
                            <td class="py-3 px-4 text-green-500">更高效</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="footer text-white py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>